package com.akhan.jplearn.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.akhan.jplearn.mapper.JPWordMapper;
import com.akhan.jplearn.service.SilenceWriteService;
import com.akhan.jplearn.util.FailMsgUtil;

/**
 * 五十音测验服务
 *
 * @author akhan
 * @date 9:28 上午 2021/6/25
 */
public class SilenceWriteServiceImpl extends BaseServiceImpl implements SilenceWriteService {
    private String nextAnswer = null;

    /**
     * 输出试题
     *
     * @return 正确答案
     * @author akhan
     * @date 9:32 上午 2021/6/25
     */
    @Override
    public String createQuest() {
        StringBuilder sb = new StringBuilder();
        int idx = RandomUtil.randomInt(0, JPWordMapper.size() - 1);
        switch (RandomUtil.randomInt(2)) {
            case 0: {
                sb.append("请输入[{}]的五十音?");
                int type = RandomUtil.randomInt(2);
                String param = null;
                if (type == 1) {
                    param = JPWordMapper.getHiragana(idx);
                } else {
                    param = JPWordMapper.getKatakana(idx);
                }
                nextAnswer = JPWordMapper.getGojyuuon(idx);
                return StrUtil.format(sb.toString(), param);
            }
            case 1: {
                sb.append("请写出发{}音的{}?");
                int type = RandomUtil.randomInt(2);
                String param = JPWordMapper.getGojyuuon(idx);
                String param2 = null;
                if (type == 1) {
                    param2 = "平假名";
                    nextAnswer = JPWordMapper.getHiragana(idx);
                } else {
                    param2 = "片假名";
                    nextAnswer = JPWordMapper.getKatakana(idx);
                }
                return StrUtil.format(sb.toString(), param, param2);
            }
            default: {
                break;
            }
        }

        return null;
    }

    @Override
    String core(String in) {
        if (StrUtil.equals(nextAnswer, in)) {
            return "回答正确！散花！";
        }
        return FailMsgUtil.randomMsg(nextAnswer);
    }

    @Override
    String prepare() {
        return createQuest();
    }

    /**
     * 服务名
     *
     * @return String 服务名
     * @author YuKang
     * @date 2021/6/25 0:16
     */
    @Override
    String getName() {
        return "五十音测验";
    }
}
